package com.nopay.dxdwphone.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import com.nopay.dxdwphone.R;

public class DialogSaveTask extends Dialog {

    public DialogSaveTask(@NonNull Activity context) {
        super(context, R.style.MyDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_save_task);

        findViewById(R.id.tv_dialog_cancel).setOnClickListener(v -> dismiss());
        findViewById(R.id.tv_dialog_confirm).setOnClickListener(v -> {
            EditText etTaskName = findViewById(R.id.et_task_name);
            String taskName = etTaskName.getText().toString();
            if(TextUtils.isEmpty(taskName)) {
                ToastUtils.showShort("请输入任务名称");
                return;
            }

            if(onSaveTaskConfirmListener != null) onSaveTaskConfirmListener.OnSaveTaskConfirm(taskName);
            etTaskName.setText("");
            dismiss();
        });
    }

    public void show(String defaultName) {
        super.show();
        EditText etTaskName = findViewById(R.id.et_task_name);
        etTaskName.setText(defaultName);
    }

    private OnSaveTaskConfirmListener onSaveTaskConfirmListener;

    public DialogSaveTask setOnSaveTaskConfirmListener(OnSaveTaskConfirmListener onSaveTaskConfirmListener) {
        this.onSaveTaskConfirmListener = onSaveTaskConfirmListener;
        return this;
    }

    public interface OnSaveTaskConfirmListener {

        void OnSaveTaskConfirm(String taskName);
    }
}
